package com.wjs.juc.Atmoic;

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

class MySource{
    public int num=0;
    public synchronized void add(){
        num++;
    }
    AtomicLong atomicLong=new AtomicLong(0);
    public void setAtomicLong(){
        atomicLong.getAndIncrement();
    }

    LongAdder longAdder=new LongAdder();
    public void setLongAdder(){
        longAdder.increment();
    }

    LongAccumulator longAccumulator=new LongAccumulator((x,y)->{
        return x+y;
    },0);
    public void setLongAccumulator(){
        longAccumulator.accumulate(1);
    }
}
public class CompareAtomicDemo {
    static int _1W=10000;
    static int size=50;
    public static void main(String[] args) throws InterruptedException {
        MySource source = new MySource();
        CountDownLatch countDownLatch1 = new CountDownLatch(size);
        CountDownLatch countDownLatch2 = new CountDownLatch(size);
        CountDownLatch countDownLatch3 = new CountDownLatch(size);
        CountDownLatch countDownLatch4 = new CountDownLatch(size);
        long startTime;
        long endTime;


        startTime=System.currentTimeMillis();
        for (int i = 0; i < size; i++) {
            new Thread(()->{
                try {
                    for(int j=0;j<_1W*100;j++){
                        source.setAtomicLong();
                    }
                } finally {
                    countDownLatch2.countDown();
                }
            }).start();
        }
        countDownLatch2.await();
        endTime=System.currentTimeMillis();
        System.out.println("AtomicLong结果为"+source.atomicLong.get()+"耗时"+(endTime-startTime)+"ms");


        startTime=System.currentTimeMillis();
        for (int i = 0; i < size; i++) {
            new Thread(()->{
                try {
                    for(int j=0;j<_1W*100;j++){
                        source.add();
                    }
                } finally {
                countDownLatch1.countDown();
                }
            }).start();
        }
        countDownLatch1.await();
        endTime=System.currentTimeMillis();
        System.out.println("synchronized结果为"+source.num+"耗时"+(endTime-startTime)+"ms");

        startTime=System.currentTimeMillis();
        for (int i = 0; i < size; i++) {
            new Thread(()->{
                try {
                    for(int j=0;j<_1W*100;j++){
                        source.setLongAdder();
                    }
                } finally {
                    countDownLatch3.countDown();
                }
            }).start();
        }
        countDownLatch3.await();
        endTime=System.currentTimeMillis();
        System.out.println("LongADDER结果为"+source.longAdder.sum()+"耗时"+(endTime-startTime)+"ms");

        startTime=System.currentTimeMillis();
        for (int i = 0; i < size; i++) {
            new Thread(()->{
                try {
                    for(int j=0;j<_1W*100;j++){
                        source.setLongAccumulator();
                    }
                } finally {
                    countDownLatch4.countDown();
                }
            }).start();
        }
        countDownLatch4.await();
        endTime=System.currentTimeMillis();
        System.out.println("synchronized结果为"+source.longAccumulator.get()+"耗时"+(endTime-startTime)+"ms");
    }
}
